/**
 * Created by Administrator on 2015/8/21.
 */
var async = require('async');
//首页有关的模型
var HomePageM = require('../models/home');
var ClzM = require('../models/classify');
var ProM = require('../models/product');
var signDayM = require('../models/signday.js');

module.exports = {
    /**
     * 显示首页数据
     * @param req
     * @param res
     * @param next
     */
    getHomepage : function(req, res){
        var uid = req.AV.user.id;
        async.parallel([
                function(callback){
                    HomePageM.getHomepage().then(function(data){
                        var lbt = []; //轮播图
                        var ggw = []; //广告位
                        var len = data.length;
                        for(var i = 0 ; i < len ; i++){
                            var d = data[i];
                            switch(d.get('cName')){
                                case 'lbt':
                                    lbt.push(d);
                                    break;
                                case 'ggw':
                                    ggw.push(d);
                                    break;
                            }
                        }
                        //ggw.sort(function(a,b){
                        //    return a.get('ordnum') - b.get('ordnum');
                        //});
                        callback(null,{lbt:lbt,ggw:ggw});
                    });
                },
                function(callback){
                    HomePageM.getHomeClassify(["grtj","grrm","grtjia","grhl"]).then(function(data){
                        async.mapSeries(data,function(item,next){
                            ProM.listByClassifyId(item.id,1,4).then(function(list){
                                next(null,{'title':item.get('title'),id:item.id,list:list});
                            });
                        },function(err,res){
                            if(err){
                                callback(err);
                            }
                            callback(null,res);
                        });

                    });
                },
                function(callback){
                    signDayM.getSignDayValue(uid).then(function(result){
                        callback(null,{code:result.code,MonthSignDay:result.MonthSignDay,AllSignDay:result.AllSignDay,SignState:result.SignState});
                    }).catch(function(result){
                        console.log(result);
                        callback(null,{code:0,MonthSignDay:0,AllSignDay:0,SignState:0});
                    });
                }
            ]
            ,function(err,results){
                if(err){
                    console.log(err);
                    return;
                }
                res.render('home',{title:'果然100',lbt:results[0].lbt,ggw:results[0].ggw,classify:results[1],signValue:results[2]});
            });
    },
    /**
     * 需要在url中传入分类的类目名称和页码
     * cname
     * page
     * @param req
     * @param res
     * @param next
     */
    listByClass:function(req,res,next){
        //获取分类的简码
        var cname = req.params.cname;
        //获取页码
        var page = req.params.p;

        async.waterfall([

            //通过cname获取cid

            function(callback){
                ClzM.getByName(cname).then(function(clz){
                    callback(null,clz);
                });
            },
            //通过cid获取商品列表
            //clz 指向前面一个函数的输出值
            function(clz,callback){
                ProM.list(clz.id,page).then(function(list){
                    callback(null,{classify:clz,list:list});
                }).catch(function(err){
                    //TODO:错误的处理
                });
            }
        ],function(err,result){
            if(err){
                //TODO:
                return;
            }
            //res.send('111');//直接把内容输出给浏览器

            res.render('p/list',{title:result.classify.get('intro'),list:result.list});
        });
    }
};
